﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PayEntity;
using System.Data.Common;

namespace PayDAL
{
    public class Class1
    {
        private Class1 c = Class1.GetClass1();

        private static Class1 GetClass1()
        {
            throw new NotImplementedException();
        }

        public Class1 QueryGoodsByCode(string code)
        {
            Class1 c = new Class1();
            string sql = "select * from GoodsInfo where goodscode like '%" + code + "%'";
            DbDataReader dr = c.GetReader(sql);

            if (dr.Read())
            {

                c.Company = Convert.ToInt32(dr["Company"]);
                c.SalePrice = Convert.ToDouble(dr["SalePrice"]);
                c.GoodsCode = dr["GoodsCode"].ToString();
                c.GoodsName = dr["GoodsName"].ToString();
                c.GoodsState = Convert.ToInt32(dr["GoodsState"]);
                c.OriginPrice = Convert.ToDouble(dr["OriginPrice"]);
                c.Stock = Convert.ToInt32(dr["Stock"]);
                c.Supplier = Convert.ToInt32(dr["Supplier"]);
                c.Unit = dr["Unit"].ToString();
            }
            dr.Close();
            c.Close();
            return c;
        }

        private void Close()
        {
            throw new NotImplementedException();
        }

        private DbDataReader GetReader(string sql)
        {
            throw new NotImplementedException();
        }

        public List<Class1> QueryListByCode(string code)
        {
            List<Class1> list = new List<Class1>();
            string sql = "select * from GoodsInfo where goodscode like '%" + code + "%'";
            DbDataReader dr = c.GetReader(sql);

            while (dr.Read())
            {
                Class1 c1 = new Class1();
                if (dr["Company"] != DBNull.Value)
                {
                    c1.Company = Convert.ToInt32(dr["Company"]);
                }
                c1.SalePrice = Convert.ToDouble(dr["SalePrice"]);
                c1.GoodsCode = dr["GoodsCode"].ToString();
                c1.GoodsName = dr["GoodsName"].ToString();
                c1.GoodsState = Convert.ToInt32(dr["GoodsState"]);
                c1.OriginPrice = Convert.ToDouble(dr["OriginPrice"]);

                if (dr["Stock"] != DBNull.Value)
                {
                    c1.Stock = Convert.ToInt32(dr["Stock"]);
                }
                if (dr["Supplier"] != DBNull.Value)
                {
                    c1.Supplier = Convert.ToInt32(dr["Supplier"]);
                }
                c1.Unit = dr["Unit"].ToString();
                list.Add(c);
            }
            dr.Close();
            c.Close();
            return list;
        }

        public string GoodsCode { get; set; }
        public string GoodsName { get; set; }
        public double OriginPrice { get; set; }
        public double SalePrice { get; set; }
        public int Stock { get; set; }
        public string Unit { get; set; }
        public int Supplier { get; set; }
        public int Company { get; set; }
        public int GoodsState { get; set; }
    }
}
